<?php

namespace app\first\controller;

use aiplat\SDK\API;
use aiplat\SDK\Configer;
use app\first\model\Collection;
use app\first\model\Fans;
use app\first\model\Filter;
use app\first\model\Friend;
use app\first\model\Message;
use app\first\model\Subscribe;
use app\first\validate\Article;
use app\first\validate\Member as MemberValidate;
use app\first\model\Member as Member;
use RongCloud\RongCloud;
use think\facade\Config;
use think\facade\Validate;

/**
 * Class User
 * @title 用户接口
 * @url /v1/user
 * @desc  有关于用户的接口
 * @route('v1/user')->allowCrossDomain()
 * @version 1.0
 * @readme 有关于用户的接口
 */
class User extends Base{
    protected $autoWriteTimestamp = true;
    //是否开启授权认证
    public $apiAuth = true;
    //附加方法
    protected $extraActionList = [];

    //跳过鉴权的方法
    protected $skipAuthActionList = ['sendCode','register','password','login','check_code','getAppConfig','chart','texttrans'];

    public function __construct(){
        parent::__construct();
    }

    /**
     * @throws \ErrorException
     * @route('v1/user/getAppConfig','get')
     */
    public function getAppConfig(){
        $flag = \config('app.is_url');
        return $this->sendSuccess(['state'=>$flag]);
    }


    /**
     * @title 用户登录
     * @method login
     * @param string $username 账号 true
     * @param string $password 密码 true
     * @route('v1/user/login','post')
     * ->allowCrossDomain()
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function login(){
        $data = request()->post();
        $memberValidate= new MemberValidate;
        if(!$memberValidate->scene('login')->check($data)){
           return $this->sendError(0,$memberValidate->getError());
        }
        $member = new Member;
        $_member = $member::where('username',$data['username'])->find();
        if(!$_member){
            return $this->sendError(0,'用户不存在');
        }
        $pwd = $this->get_password($data['password'],0);
        if($_member['password']!=$pwd){
            return $this->sendError(0,'密码不正确');
        }
        $_member1 = $member::getMember($_member['id']);
        $token = self::jwt($_member1);
        $_member1['token'] = $token;
        return $this->sendSuccess($_member1);
    }

    /**
     * @title 修改用户信息
     * @method save
     * @param string $nickname 昵称 true
     * @param string $information 介绍 true
     * @param int $sex 性别 true 1 1男,2女,0未知
     * @param string $head 头像 true  头像地址
     * @param string $head
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function save($nickname='',$information='',$sex=1,$head=''){
        $data = request()->param();
        $member = new Member();
        $valid = new \app\first\validate\Member();
        if(!$valid->scene('edit')->check($data)){
            return $this->sendError(0,$valid->getError());
        }
        $info = $member->find(self::$uid);
        $info->nickname = $nickname;
        $info->information = $information;
        $info->sex = $sex;
        $info->head = $head;
        if(!$info->save()){
            return $this->sendError(0,'修改失败');
        }
        return $this->sendSuccess('','修改成功');
    }

    /***
     * @title 发送验证码
     * @method send_code
     * @param string $to 手机号/邮箱 true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function sendCode($to=''){
        if(!$to){
            return $this->sendError(0,lang('empty',[lang('receive')]));
        }
        if(strstr($to,'@')===false){
            if(!Validate::is($to,'/^1[34578]\d{9}$/')){
                return $this->sendError(0,lang('unrequire',[lang('phone'),lang('phone')]));
            }
            $arr = send_sms($to);
            return $this->sendSuccess($arr);
        }else{
            if(!Validate::is($to,'email')){
                return $this->sendError(0,lang('unrequire',[lang('phone'),lang('email')]));
            }
            $_code = no_random(0,9,6);
            $html = "【".$this->site['title']."】:您本次的验证码:".
                $_code.",有效时间为15分钟.如果您没有使用【".$this->site['title']
                ."】相关产品,请自动忽略此邮件谢谢:)";

            if(!think_send_mail($to, $to,"【".$this->site['title']."】",$html)){
                return $this->sendError(0,lang('error',['']));
            }
            cookie($_code.'_session_code', $_code, 60*15);
            return $this->sendSuccess('',lang('done',[lang('send')]));
        }
    }

    /**
     * @title 检查验证码是否正确
     * @param string $code 验证码 true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml*
     *@route('v1/user/check_code','get')->allowCrossDomain()
     */
    public function check_code($code=''){
        if(!$code){
            return $this->sendError(0,lang('empty',[lang('verify')]));
        }
        $data = $this->check_verify($code);
        return $this->sendSuccess($data);
    }

    /**
     * @title 修改手机号
     * @method phone
     * @param string $phone 手机号 true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     * @return Object User 用户信息
     *
     * @route('v1/user/phone','post')->allowCrossDomain()
     */
    public function phone($phone=''){
        if(!$phone){
            return $this->sendError(0,'请输入手机号');
        }
        $member = Member::get(self::$uid);
        if(!$member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $member->tel = $phone;
        if(!$member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('success',[lang('upgrade')]));
    }

    /**
     * @title 修改头像
     * @method head
     * @param Object/string head File文件/base64 true
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/head','post')
     */
    public function head(){
        if(!request()->isPost()){
            return $this->sendError(0,lang('error',[lang('unrequest')]));
        }
        $upload = new Uploadify();
        $_file = $upload->upload_head();
        if(!$_file['status']){
            return $this->sendError(0,$_file['message']);
        }
        $member = new Member;
        $m  = $member::getMember(self::$uid);
        if(!$m){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $m->head = $_file['path'];
        if(!$m->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        $_m = $member::getMember(self::$uid);
        return $this->sendSuccess($_m);
    }


    /**
     * @title 设置签名
     * @method info
     * @param string $info 个性签名 true
     * @return Object 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/info','post')->allowCrossDomain()
     */
    public function info($info=''){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('info')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->information=$info;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        $_m = $member::getMember(self::$uid);
        return $this->sendSuccess($_m);
    }

    /**
     * @title 获取用户信息
     * @method profile
     * @param int $id 用户id false  不填写获取自己信息
     * @param int $rid 查看用户id false
     * @route('v1/user/profile','get')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @return User Object 用户信息
     */
    public function profile($id=0,$rid=0){
        $member = new Member;
        if($rid == $id){
            $info = $member::getMember($id);
            $info['is_subscribe'] = $rid?$member->issub($id,$rid):0;
        }else{
            $info = $member::getMember($rid);
            $info['is_subscribe'] = $rid?$member->issub($id,$rid):0;
        }
        return $this->sendSuccess($info);
    }

    /**
     * @title 修改密码
     * @method password
     * @param string $password 密码 true
     * @param string $phone 手机号 true
     * @route('v1/user/password','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @return json data 修改结果
     */
    public function password($password='',$phone=''){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$phone){
            return $this->sendError(0,'请填写手机号');
        }
        if(!$validate->scene('password')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member->where('username','eq',$phone)->find();
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->password = $this->get_password($password,0);
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     * @title 设置用户喜好
     * @method hobbies
     * @param string $hobbies 用户喜好 true
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/hobbies','post')->allowCrossDomain()
     */
    public function hobbies($hobbies=''){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('hobby')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->hobbies = $hobbies;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        $_m = Member::getMember(self::$uid);
        return $this->sendSuccess($_m,lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     *  @title 创建用户
     * @return Object User  注册用户
     * @route('v1/user/register','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function register(){
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $appkey = Config::get('rong.appKey');
        $appSecret = Config::get('rong.appSecret');
        $ro = new RongCloud($appkey,$appSecret);
        $member = new Member;
        if($member->where('username','eq',$data['username'])->count()){
            return $this->sendError(0,'已经注册了');
        }
        $data['nickname'] = '用户'.$this->getSecret();
        $data['head']= 'https://'.request()->host().'/images/10104372.gif';
        $data['password'] = $this->get_password($data['password'],0);
        $data['create_time']=time();
        if(!$id = $member->insertGetId($data)){
            return $this->sendError(0,$member->getError());
        }
        $token = $ro->user()->getToken($id,$data['username'],'http://www.rongcloud.cn/images/logo.png');
        $token = json_decode($token,true);
        if($token['code']){
            $member->where('id','eq',$id)->setField('rong_token',$token['token']);
        }
        $_member = $member::getMember($id);
        return $this->sendSuccess(['status'=>1,'message'=>lang('done',[lang('register')]),'data'=>$_member]);
    }

    /**
     * @title  修改用户昵称
     * @param string $nickname 昵称
     * @return Object User 用户
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/nickname','post')->allowCrossDomain()
     */
    public function nickname($nickname=''){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('nickname')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->nickname = $nickname;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     * @title 修改性别
     * @method sex
     * @param int $sex 性别 ture -1 -1未知,0男,1女
     * @return Object User 用户信息
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     * @route('v1/user/sex','post')->allowCrossDomain()
     */
    public function sex($sex=-1){
        if(!request()->isPost()){
            return $this->sendError(0,lang('unrequest'));
        }
        $data = request()->post();
        $validate = new MemberValidate;
        if(!$validate->scene('sex')->check($data)){
            return $this->sendError(0,$validate->getError());
        }
        $member = new Member;
        $_member = $member::get(self::$uid);
        if(!$_member){
            return $this->sendError(0,lang('unalready',[lang('user')]));
        }
        $_member->sex = $sex;
        if(!$_member->save()){
            return $this->sendError(0,lang('error',[lang('upgrade')]));
        }
        return $this->sendSuccess('',lang('done',[lang('success',[lang('upgrade')])]));
    }

    /**
     * @title 发布帖子
     * @method publish
     * @param string $title 帖子主题 ture
     * @param string $cid 帖子分类 ture
     * @param string $keywords 帖子关键词 false  多个使用','隔开
     * @param string $description 帖子介绍 false
     * @param string $cover 帖子封面 ture
     * @param string $image 帖子图片 false  多个使用','隔开
     * @param string $content 帖子内容 ture
     * @return json data 结果
     * @route('v1/user/publish','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function publish(){
        $data = request()->post();
        $valid = new Article();
        if(!$valid->check($data)){
            return $this->sendError(0,$valid->getError());
        }
        $data['mid'] = self::$uid;
        $data['create_time'] = time();
        $model = new \app\first\model\Article();
        if(!$model->insert($data)){
            return $this->sendError(0,'发布失败');
        }
        return $this->sendSuccess('','发布成功');
    }

    /**
     * @title 我的发布
     * @method mypublish
     * @param int $limit 每页条数 true 10
     * @param int $page 当前页数 true 1
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     * @route('v1/user/mypublish','post')->allowCrossDomain()
     */
    public function mypublish($limit=10,$page=1){
        $model = new \app\first\model\Article();
        $id = input('uid',self::$uid);
        $map[]=['mid','eq',$id];
        $list = $model->getList($map,'create_time desc',$limit);
        if(!$list){
            $list = [];
        }
        return $this->sendSuccess($list);
    }

    /**
     * @title 查询用户
     * @method queryuser
     * @param string $q 搜索关键词 true 用户名或用户昵称
     * @route('v1/user/queryuser','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function queryuser($q=''){
        $user = new Member();
        $list = $user->field('update_time',true)
            ->whereLike('username|nickname',"%{$q}%")
            ->select();
        if(!$list){
            $list = new \stdClass();
        }
        return $this->sendSuccess($list);
    }

    /**
     * @title 添加好友
     * @method addfriends
     * @param int $uid  好友id true
     * @route('v1/user/addfriends','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function addfriends($uid=0){
        $friend = new Friend();
        $result = $friend->addNew(self::$uid,$uid,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','添加成功，等待好友审核');
    }

    /**
     * @title 好友列表
     * @method friends
     * @param int $type 类型 true 1 1正常好友,2黑名单好友
     * @param int $limit 没有条数 true 10
     * @param int $page 当前页数 true 1
     * @route('v1/user/friends','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function friends($type=1,$limit=10,$page=1){
        $friend = new Friend();
        $map[]=['mid|rid','eq',self::$uid];
        $map[]=['is_black','eq',($type==1 ? 0 : 1)];
        //$map[]=['status','eq',2];
        $list = $friend->getList($map,'create_time desc',$limit);
        if(!$list){
            $list = [];
        }
        return $this->sendSuccess($list);
    }

    /**
     * @title 通过好友
     * @method checkfriends
     * @param int $id 列表id
     * @param int $type 审核类型 true 2通过,3拒绝
     * @param string $info 不通过原因 false 不通过时必填
     * @route('v1/user/checkfriends','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function checkfriends($id=10,$type=1,$info=''){
        $friend = new Friend();
        $result = $friend->check($id,$type,$info,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 好友设置名单
     * @method isblack
     * @param int $uid 用户id true
     * @param int $type 类型 true 1 1设置黑名单,2取消黑名单,3删除好友
     * @route('v1/user/isblack','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function isblack($uid=10,$type=1){
        $friend = new Friend();
        $result = $friend->black(self::$uid,$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 收藏
     * @method collection
     * @param int $aid 文章id true
     * @param int $type 类型 true 1 1收藏,2取消收藏
     * @route('v1/user/collection','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collection($aid=0,$type=1){
        $collection = new Collection();
        $result = $collection->collection(self::$uid,$aid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 我的收藏
     * @method mycollection
     * @param int $limit 每页数量 10 true
     * @param int $page 当前页数 1 true
     * @route('v1/user/mycollection','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function mycollection($limit=10,$page=1){
        $collection = new Collection();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        $result = $collection->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }

    /**
     * @title 关注
     * @method subscribe
     * @param int $uid 用户id true
     * @param int $type 类型 true 1 1收藏,2取消收藏
     * @route('v1/user/subscribe','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function subscribe($uid=0,$type=1){
        $collection = new Subscribe();
        $result = $collection->subscribe(self::$uid,$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 我的关注
     * @method mysubscribe
     * @param int $limit 每页数量 10 true
     * @param int $page 当前页数 1 true
     * @route('v1/user/mysubscribe','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function mysubscribe($limit=10,$page=1){
        $collection = new Subscribe();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        $result = $collection->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }

    /**
     * @title 添加粉丝
     * @method fans
     * @param int $uid 粉丝id true
     * @param int $type 类型 true
     * @route('v1/user/fans','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function fans($uid=0,$type=1){
        $collection = new Fans();
        $result = $collection->subscribe(self::$uid,$uid,$type,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 我的粉丝
     * @method myfans
     * @param int $limit 每页数量 10 true
     * @param int $page 当前页数 1 true
     * @route('v1/user/myfans','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function myfans($limit=10,$page=1){
        $collection = new Fans();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        $result = $collection->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }

    /**
     * @title 帖子评论
     * @method message
     * @param string $aid 帖子id true
     * @param string $images 图片 false  多个使用','隔开
     * @param string $content 评论内容 true
     * @route('v1/user/message','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function message(){
        $data = request()->param();
        $data['mid']=self::$uid;
        $message = new Message();
        $result = $message->message($data,$out);
        if(!$result){
            return $this->sendError(0,$result);
        }
        return $this->sendSuccess(0,'操作成功');
    }

    /**
     * @title 我的评论
     * @method mymessage
     * @param int $limit 每页数量 true 10
     * @param int $page 当前页 true 1
     * @route('v1/user/mymessage','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function mymessage($limit=10,$page=1){
        $message = new Message();
        $id = input('uid',self::$uid);
        $map[] = ['mid','eq',$id];
        $result = $message->getList($map,'create_time desc',$limit);
        if(!$result){
            $result = [];
        }
        return $this->sendSuccess($result);
    }

    /**
     * @title 用户帖子发布
     * @method myarticle
     * @param int $id 用户id false  不填获取自己
     * @param int $limit
     * @param int $page
     * @route('v1/user/myarticle','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\exception\DbException
     */
    public function myarticle($id=0,$limit=10,$page = 1){
        $model = new \app\first\model\Article();
        $data = request()->post();
        $id = isset($data['id'])?$data['id']:self::$uid;
        $limit = isset($data['limit'])?$data['limit']:10;
        $map[] = ['mid','eq',$id];
        $order="create_time desc";
        $list = $model->getList($map,$order,$limit);
        return $this->sendSuccess($list);
    }

    /**
     * @title 屏蔽用户
     * @method filter
     * @param int $uid 屏蔽用户id true
     * @route('v1/user/filter','post')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     */
    public function filter($uid=0){
        $filter = new Filter();
        $result = $filter->addNew(self::$uid,$uid,$out);
        if(!$result){
            return $this->sendError(0,$out);
        }
        return $this->sendSuccess('','操作成功');
    }

    /**
     * @title 腾讯闲聊机器人
     * @method chart
     * @param string $text 聊天内容 true
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     *
     * @route('v1/user/chart','post')->allowCrossDomain()
     */
    public function chart($text=''){
        $config = Config::get('aiplat.');
        Configer::setAppInfo($config['app_id'],$config['app_key']);
        $params = [
            'time_stamp'=>time(),
            'nonce_str'=>self::secret(),
            'session'=>self::secret(6),
            'question'=>$text
        ];
        $response = API::nlp_textchat($params);
        return $this->sendSuccess($response);
    }

    /**
     * @title 翻译
     * @method texttrans
     * @param string $text 翻译内容 true  UTF-8编码
     * @param int $type 翻译类型 true 0
     * @route('v1/user/texttrans','post')->allowCrossDomain()
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function texttrans($text='',$type=0){
        $config = Config::get('aiplat.');
        Configer::setAppInfo($config['app_id'],$config['app_key']);
        $params = [
            'type'=>$type,
            'text'=> mb_convert_encoding($text, 'UTF-8','auto')
        ];
        $response = API::texttrans($params);
        return $this->sendSuccess($response);
    }
}